package jp.co.toshiba.ffsa.action.csas;

import java.util.ArrayList;

import javax.annotation.Resource;

import jp.co.toshiba.ffsa.action.common.CommonAction;
import jp.co.toshiba.ffsa.form.csas.Csas103Form;
import jp.co.toshiba.ffsa.service.common.ConstantValue;
import jp.co.toshiba.ffsa.service.csas.Csas103Service;

import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.seasar.struts.annotation.ActionForm;
import org.seasar.struts.annotation.Execute;
import org.seasar.struts.util.ActionMessagesUtil;
import org.seasar.struts.util.MessageResourcesUtil;
import org.seasar.struts.util.RequestUtil;

/**
 * 新規ID登録>入力内容確認
 * @author npc
 */
public class Csas103Action extends CommonAction {
    /** 画面ID */
    public String screenID = "CSAS103";
    @ActionForm
    @Resource
    protected Csas103Form csas103Form;

    @Resource
    protected Csas103Service csas103Service;

    /**
     * 初期処理
     * @return 画面遷移先
     * @throws Exception
     */
    @Execute(validator = false)
    public String index() throws Exception {
        // リファラ判定
        if(this.judgeRefferer()){
            // セッションタイムアウト判定
            if(this.judgeSession()){
                return "/csas/csas103.jsp";
            }else{
                return this.topTransition;
            }
        }else{
            return this.topTransition;
        }
    }

    /**
     * 入力内容登録処理
     * @return 画面遷移先
     * @throws Exception
     */
    @Execute(validator = true, input="/csas/csas103.jsp")
    public String insertRegInfo () throws Exception{

        String transition = "/csas/csas103.jsp"; // 遷移先
        String result = "";
        try{
            // リファラ判定
            if(this.judgeRefferer()){
                // セッションタイムアウト判定
                if(this.judgeSession()){
                    csas103Service = new Csas103Service();
                    csas103Service.setEmailAddress(this.csas103Form.emailAddress);
                    csas103Service.setFirstName(this.csas103Form.firstName);
                    csas103Service.setLastName(this.csas103Form.lastName);
                    csas103Service.setPassword(this.csas103Form.newPassword);
                    csas103Service.setCompany(this.csas103Form.company);
                    csas103Service.setDepartment(this.csas103Form.department);
                    csas103Service.setPhone(this.csas103Form.phone);
                    csas103Service.setRegion(this.csas103Form.region);
                    csas103Service.setCountry(this.csas103Form.country);
                    csas103Service.setCity(this.csas103Form.city);

                    // データ更新
                    if(csas103Service.userRegInfo()){
                        // メール送信
                        if(csas103Service.mailSend()){
                            transition = "/csas/csas104.jsp";
                            result = ConstantValue.success;
                        } else {
                            ActionMessages messages = new ActionMessages();
                            messages.add("mailSendFailed", new ActionMessage(csas103Service.getErrorCode(), true));
                            ActionMessagesUtil.addErrors(RequestUtil.getRequest().getSession(true), messages);
                            transition = "/csas/csas103.jsp";
                            result = ConstantValue.fail;
                        }
                    }else{
                        ActionMessages messages = new ActionMessages();
                        messages.add("userRegInfoFailed", new ActionMessage(csas103Service.getErrorCode(), true));
                        ActionMessagesUtil.addErrors(RequestUtil.getRequest().getSession(true), messages);
                        transition = "/csas/csas103.jsp";
                        result = ConstantValue.fail;
                    }

                    if(ConstantValue.success.equals(result)){
                        // 操作ログ記述
                        ArrayList<ArrayList<String>> parameters = new ArrayList<ArrayList<String>>();
                        ArrayList<String> parameterNames = new ArrayList<String>();
                        ArrayList<String> values = new ArrayList<String>();
                        parameterNames.add(MessageResourcesUtil.getMessage("mail_address"));
                        values.add("-");
                        parameterNames.add(MessageResourcesUtil.getMessage("new_password"));
                        values.add("-");
                        parameterNames.add(MessageResourcesUtil.getMessage("Reenter_new_password"));
                        values.add("-");
                        parameterNames.add(MessageResourcesUtil.getMessage("first_name"));
                        values.add(this.csas103Form.firstName);
                        parameterNames.add(MessageResourcesUtil.getMessage("last_name"));
                        values.add(this.csas103Form.lastName);
                        parameterNames.add(MessageResourcesUtil.getMessage("company"));
                        values.add(this.csas103Form.company);
                        parameterNames.add(MessageResourcesUtil.getMessage("department"));
                        values.add(this.csas103Form.department);
                        parameterNames.add(MessageResourcesUtil.getMessage("phone"));
                        values.add(this.csas103Form.phone);
                        parameterNames.add(MessageResourcesUtil.getMessage("region"));
                        values.add(this.csas103Form.region);
                        parameterNames.add(MessageResourcesUtil.getMessage("country"));
                        values.add(this.csas103Form.country);
                        parameterNames.add(MessageResourcesUtil.getMessage("city"));
                        values.add(this.csas103Form.city);
                        parameterNames.add(MessageResourcesUtil.getMessage("confirm_corporation"));
                        values.add("yes");
                        parameters.add(parameterNames);
                        parameters.add(values);
                        this.entryOperationHistory(this.screenID, "submit", parameters, result, csas103Service.getUserID());
                    }

                    return transition;

                }else{
                    return this.topTransition;
                }
            }else{
                return this.topTransition;
            }
        }catch(Exception e){
            this.errorLogWriter(e, "insertRegInfo", this.screenID);
            throw e;
        }
    }
}